package automaticExecution

import (
	"gowork/shoppingBodyAward/model"
	cron "github.com/robfig/cron/v3"
	"gowork/shoppingBodyAward/utils"
	"time"
)

func init(){
	var lkt *model.LinkageTask
	var lkm *model.LinkageMessage
	var a *model.Account
	var lkfs *model.LinkageFailureSucces
	var cr *model.CallRecord

	w := new(utils.Worker)

	cronLinkage := cron.New()
  	task := func() {
		lktList := lkt.QueryLinkageTask()
		for _, lktv := range lktList{
			if lktv.LinkageState == 1{
				elkm := lkm.QueryLinkageMessageById(lktv.LinkageMessageId)
				ma := a.QueryAccountById(elkm.RecvAccountId)
				var lkmManager = model.LinkageMessage{
					LinkageMessageId: w.GetId(),
					MessageTypeId: elkm.MessageTypeId,
					SendAccountId : elkm.SendAccountId,
					SendAccountName : elkm.SendAccountName,
					RecvAccountId : ma.AccountId,
					RecvAccountName : ma.AccountName,
					CustomerId : elkm.CustomerId,
					CustomerName : elkm.CustomerName,
					SendMessageBody : "请尽快回复！",
					CreatedAt : time.Now(),
					UpdatedAt : time.Now(),
				}
				rLkmManager := lkm.InsertLinkageMessage(&lkmManager)
				lktv.LinkageState = 2
				lktv.EmployeeOrSupervisor = 2
				lktv.LinkageMessageId = rLkmManager.LinkageMessageId
				hh, _ := time.ParseDuration("4h")
				lktv.TermOfValidity = lktv.CreatedAt.Add(hh)
				lkt.UpdateTermOfValidity(lktv)

			}else if lktv.LinkageState == 2{
				lktv.IsOver = 2
				lkt.UpdateTermOfValidity(lktv)
				var newLkfs = model.LinkageFailureSucces{
					LinkageFailureSuccesId: w.GetId(),
					FailureSuccesState : 2,
					CustomerId : lktv.CustomerId,
					CustomerName : lktv.CustomerName,
					LinkageTaskId : lktv.LinkageTaskId, 
					CreatedAt : time.Now(),
					UpdatedAt : time.Now(),
				} 
				lkfs.InsertLinkageFailureSucces(&newLkfs)

			}else if lktv.LinkageState == 3{
				if len(cr.QueryCallRecord(lktv.CustomerId, lktv.AccountId, lktv.ShoppingReply, lktv.TermOfValidity)) > 0{
					lktv.IsOver = 2
					lkt.UpdateTermOfValidity(lktv)
					var newLkfs = model.LinkageFailureSucces{
						LinkageFailureSuccesId: w.GetId(),
						FailureSuccesState : 1,
						CustomerId : lktv.CustomerId,
						CustomerName : lktv.CustomerName,
						LinkageTaskId : lktv.LinkageTaskId, 
						CreatedAt : time.Now(),
						UpdatedAt : time.Now(),
					}
					lkfs.InsertLinkageFailureSucces(&newLkfs)
				}else{
					elkm := lkm.QueryLinkageMessageById(lktv.LinkageMessageId)
					ma := a.QueryAccountById(elkm.SendAccountId)
					var lkmManager = model.LinkageMessage{
						LinkageMessageId: w.GetId(),
						MessageTypeId: elkm.MessageTypeId,
						SendAccountId : elkm.SendAccountId,
						SendAccountName : elkm.SendAccountName,
						RecvAccountId : ma.AccountId,
						RecvAccountName : ma.AccountName,
						CustomerId : elkm.CustomerId,
						CustomerName : elkm.CustomerName,
						SendMessageBody : "请尽快给客户致电！",
						CreatedAt : time.Now(),
						UpdatedAt : time.Now(),
					}
					rLkmManager := lkm.InsertLinkageMessage(&lkmManager)
					lktv.LinkageState = 4
					lktv.EmployeeOrSupervisor = 2
					lktv.LinkageMessageId = rLkmManager.LinkageMessageId
					hh, _ := time.ParseDuration("36h")
					lktv.TermOfValidity = lktv.CreatedAt.Add(hh)
					lkt.UpdateTermOfValidity(lktv)
					}

			}else if lktv.LinkageState == 4{
				if len(cr.QueryCallRecord(lktv.CustomerId, lktv.AccountId, lktv.ShoppingReply, lktv.TermOfValidity)) > 0{
					lktv.IsOver = 2
					lkt.UpdateTermOfValidity(lktv)
					var newLfs = model.LinkageFailureSucces{
						LinkageFailureSuccesId: w.GetId(),
						FailureSuccesState : 1,
						CustomerId : lktv.CustomerId,
						CustomerName : lktv.CustomerName,
						LinkageTaskId : lktv.LinkageTaskId, 
						CreatedAt : time.Now(),
						UpdatedAt : time.Now(),
					} 
					lkfs.InsertLinkageFailureSucces(&newLfs)
				}else{
					lktv.IsOver = 2
					lkt.UpdateTermOfValidity(lktv)
					var newLfs = model.LinkageFailureSucces{
						LinkageFailureSuccesId: w.GetId(),
						FailureSuccesState : 2,
						CustomerId : lktv.CustomerId,
						CustomerName : lktv.CustomerName,
						LinkageTaskId : lktv.LinkageTaskId, 
						CreatedAt : time.Now(),
						UpdatedAt : time.Now(),
					} 
					lkfs.InsertLinkageFailureSucces(&newLfs)
				}
			}	
		}
	}

	// 添加定时任务, * * * * * 是 crontab,表示每分钟执行一次
	cronLinkage.AddFunc("* * * * *", task)
  	// 启动定时器
	cronLinkage.Start()
}